package com.dao.impl;

import com.bean.BookBean;
import com.dao.BooksDao;
import com.util.JdbcUtil;
import com.util.PageUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BooksDaoImpl implements BooksDao {

    @Override
    public int insert(BookBean bookBean) throws SQLException {
        return 0;
    }

    @Override
    public List<BookBean> queryData(BookBean t) throws IllegalAccessException, SQLException, InstantiationException {
         String sql  = "SELECT * from dd_books LIMIT 1,10;";
         return JdbcUtil.queryData(sql,BookBean.class);
    }

    @Override
    public int update(BookBean b) throws SQLException {
        String sql = "update dd_books set book_name=?,isbn=?,author=?," +
                "   publish=?,publish_date=?,pages=?,book_price=?,mini_price=?,pic_url=?,counts=?,book_url=?,type_name=?" +
                " where book_id=?";

        return  JdbcUtil.insertOrUpdateOrDelete(sql,new Object[]{
                b.getBook_name(),b.getIsbn(),b.getAuthor(),
                b.getPublish(),b.getPublish_date(),b.getPages(),b.getBook_price(),b.getMini_price(),b.getPic_url(),b.getCounts()
                ,b.getBook_url(),b.getType_name(),b.getBook_id()
        });
    }

    @Override
    public int deleteById(Object id) throws SQLException {
        String sql  = "delete  from dd_books where book_id=?";
        return JdbcUtil.insertOrUpdateOrDelete(sql,new Object[]{id});
    }

    @Override
    public PageUtil<BookBean> queryDataBypage(int currentPgae, int pageSize, BookBean bookBean) throws IllegalAccessException, SQLException, InstantiationException {
        //SELECT * from dd_books where 1=1
        //SELECT * from dd_books where 1=1 and book_name like ?
        //SELECT * from dd_books where 1=1 and book_name like ?  and type_name like ?
        //SELECT * from dd_books where 1=1   and type_name like ?

        String sql  = "SELECT * from dd_books where 1=1";

        ArrayList values = new ArrayList();
        if (bookBean!=null){
            if (bookBean.getBook_name()!=null  &&  !"".equals( bookBean.getBook_name())){
                 sql +=" and book_name like ? ";
                values.add( "%"+bookBean.getBook_name() +"%");
            }
            if (bookBean.getType_name()!=null && !"".equals( bookBean.getType_name())){
                sql +=" and type_name like ? ";
                values.add( "%"+bookBean.getType_name() +"%");
            }
        }

        return JdbcUtil.queryDataByPage(sql,BookBean.class,  currentPgae,pageSize ,values.toArray());
    }
}
